/*!
 * Copyright (c) 2023-present StarDreamNet
 */

(document.loader || (document.loader = [])).push(["home", class {
    constructor() {
        this.postData = null;
        this.newThings = null;
    };

    init_(require) {
        const { resize, dateSpanString } = require("index");
        const { loadPostsByData } = require("postlist");

        this.resize = resize;
        this.dateSpanString = dateSpanString;
        this.loadPostsByData = loadPostsByData.bind(require("postlist"));

        this.loadAll();
        this.bannerHeightUpdate();
    };

    // —————————————————————— 海报 ——————————————————————
    bannerHeightUpdate() {
        var banner = $(".app-main-begin-banner");
        banner.height(banner.width() * (375 / 781));
        requestAnimationFrame(this.bannerHeightUpdate.bind(this));
    }

    // —————————————————————— 我们的帖子 ——————————————————————
    loadOurPostsStart() { // 开始
        $(".app-cards").attr("load", "").html("").append($("<p class='load'>加载中</p>"));
        this.resize();
    };
    loadOurPostsEnd() { // 完成
        $(".app-cards").removeAttr("load", "").find("p.load").remove();
        this.resize();
    };
    loadOurPostsError(msg) { // 错误
        this.loadOurPostsEnd();
        $(".app-cards").attr("load", "").attr("error", "").append($("<p>").text(msg || '加载出错'));
        this.resize();
    };
    renderOurPosts() { // 渲染
        var data = this.postData;
        var cards = $('.app-cards');
        this.loadPostsByData(data, cards);
        this.resize();
    };

    // —————————————————————— 我们的新品 ——————————————————————
    loadNewThingsStart() { // 开始
        $(".app-main-begin-newthing-content").attr("load", "").html("").append($("<p class='load'>加载中</p>"));
        this.resize();
    };
    loadNewThingsEnd() { // 完成
        $(".app-main-begin-newthing-content").removeAttr("load", "").find("p.load").remove();
        this.resize();
    };
    loadNewThingsError(msg) { // 结束
        this.loadNewThingsEnd();
        $(".app-main-begin-newthing-content").attr("load", "").attr("error", "").append($("<p>").text(msg || '加载出错'));
        this.resize();
    };
    renderNewThings() { // 渲染
        for (let val of this.newThings) {
            let a = $("<a></a>");
            a.text(val.name);
            a.attr("href", val.url);
            a.attr("alt", val.name);
            $('.app-main-begin-newthing-content').append(a);
        }
    };

    loadAll() { // 加载全部
        this.loadOurPostsStart();
        $.ajax('/api/shequ/get_home_post_list?t=' + Date.now(), {
            contentType: 'application/json'
        }).done((response) => {
            if (response.code == 200) {
                this.postData = response.data;

                this.loadOurPostsEnd();
                this.renderOurPosts()
            } else {
                this.loadOurPostsError(response.msg);
            }
        }).fail(() => {
            this.loadOurPostsError();
        });

        this.loadNewThingsStart();
        $.ajax('/api/get_new_things?t=' + Date.now(), {
            contentType: 'application/json'
        }).done((responseText) => {
            var data = responseText;
            if (data.code == 200) {
                this.newThings = data.data;

                this.loadNewThingsEnd();
                this.renderNewThings();
            } else {
                this.loadOurPostsError(data.msg);
            }
        }).fail(() => {
            this.loadNewThingsError();
        });
    };
}])